var events = require("events");
var EventEmitter = events.EventEmitter;

function Dog(name,energy){

    // 不完全继承
    EventEmitter.call(this);
    this.name = name;
    this.energy = energy;
    //function函数，函数内的this指向取决于调用主体
    //箭头函数，函数内this指向函数定义的上下文
    var intervalId = setInterval(()=>{
        // 事件触发的方式
        if(this.energy >= 0){
            this.emit("bark");
            this.energy--;
        }else{
            clearInterval(intervalId);
        }
    },1000)

}

// 获取EventEmitter.prototype属性
for(var i in EventEmitter.prototype){
    Dog.prototype[i] = EventEmitter.prototype[i];
}

module.exports = Dog;